package softswitch.job.process;

import java.util.List;

import javax.ejb.AccessTimeout;
import javax.ejb.EJB;
import javax.ejb.Singleton;

import pbx.model.Cdr;
import pbx.service.CdrService;
import softswitch.model.CallDetail;
import softswitch.ws.CdrProcessService;
import softswitch.ws.CallSummaryService;
import softswitch.ws.SoftswitchService;


@Singleton
public class ProcessCdrWorker {

	@EJB
	CdrService cdrService;
	
	@EJB
	SoftswitchService softSwitchService;
	
	@EJB
	CdrProcessService cdrProcessService;
	
	@EJB
	CallSummaryService callSummaryService;
	
	
	@AccessTimeout(0)
	public void processCdrs() throws InterruptedException {
		
		System.out.println("RUNNING REPROCESS CDR WORKER");
		
		List<Cdr> cdrs =  cdrService.listForReprocess(100);
		if(cdrs!=null){
			Integer size = cdrs.size();
			Integer processed = 0;
			System.out.println("CDR's SIZE " + size);
			for(Cdr cdr:cdrs){
				
				CallDetail calldetail = cdrProcessService.process(cdr);
				System.out.println("CALLDETAIL -------------------------   "+calldetail);
				if(calldetail != null){
					  System.out.println("UPDATING SUMMARIES");
					  callSummaryService.updatePackageSummary(calldetail);
					  callSummaryService.updateCallSummaryRouteGroup(calldetail);
					//callSummaryService.updateCallSummaryMonth(calldetail);
				}
				processed ++;
				System.out.println("CDRS PROCESCED " + processed + "/" + size);
			}
		}
		
	}//reprocessCDR()
	
	

}